/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rad.Java.MotelPlus.List;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rad.Java.Common.Class.DefaultList;
import rad.Java.Common.Class.IList;
import rad.Java.Common.DB.GenerateSql;
import rad.Java.MotelPlus.Class.Product;

/**
 *
 * @author Renato ferreira dos santos
 */
public class Products extends DefaultList implements IList{
    // <editor-fold defaultstate="collapsed" desc="Declaration of variables">
        private Product p;
    // </editor-fold>

    public Products()
    {
        p = new Product();
        _gsql = new GenerateSql();
        _gsql.setTable("product");
    }

    // <editor-fold defaultstate="collapsed" desc="Declaration of functions">

        public boolean fillByCode(String value)
        {
            _gsql.prepare();
            _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
            _gsql.setValue("","*");
            _gsql.setConditionalWhere("product_code LIKE '%"+value+"%'");
            _gsql.setAditionalWhere("ORDER BY product_name");

            return mountList();            
        }

        public boolean fillByName(String value)
        {
            _gsql.prepare();
            _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
            _gsql.setValue("","*");
            _gsql.setConditionalWhere("product_name LIKE '%"+value+"%'");
            _gsql.setAditionalWhere("ORDER BY product_name");

            return mountList();            
        }

        public boolean fillList() {
            _gsql.prepare();
            _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
            _gsql.setValue("","*");
            _gsql.setAditionalWhere("ORDER BY product_name");

            return mountList();
        }

         public Product get(int index)
        {
            return (Product) _items.get(index);
        }

        private boolean mountList()
        {
            try {
                ResultSet rs;
                if (!_gsql.execute(false)) {
                    return false;
                }
                clear();
                while (_gsql.next()) {
                    Product product = new Product();
                    product.get(_gsql.getResultSet());
                    _items.add(product);
                }
                return true;
            }
            catch (SQLException ex) {
                Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
    // </editor-fold>
}
